#include    "examanswer.h"
#include    <stdio.h>
#include    <string.h>
#include    <iostream>


const unsigned int ExamAnswer::SINGLE_CHOICE_NUMBER = 60;
const unsigned int ExamAnswer::MULTIPLE_CHOICE_NUMBER = 20;
const std::string ExamAnswer::NO_ANSWER = "";

ExamAnswer::ExamAnswer()
{
    this->singleChoiceSolution.clear();
    this->multipleChoiceSolution.clear();
    this->initSingleChoiceSolution();
    this->initMultipleChoiceSolution();
}

ExamAnswer::~ExamAnswer()
{
    this->singleChoiceSolution.clear();
    this->multipleChoiceSolution.clear();
}

void ExamAnswer::initSingleChoiceSolution()
{
    for(unsigned int i = 1; i <= SINGLE_CHOICE_NUMBER ; i++)
    {
        this->singleChoiceSolution[i] = NO_ANSWER;
    }
}

void ExamAnswer::initMultipleChoiceSolution()
{
    std::vector<std::string> emptyVector;
    emptyVector.clear();

    for(unsigned int i = SINGLE_CHOICE_NUMBER; i <= SINGLE_CHOICE_NUMBER + MULTIPLE_CHOICE_NUMBER ; i++)
    {
        this->multipleChoiceSolution[i] = emptyVector;
    }
}

void ExamAnswer::showExamSolution()
{
    this->showSingleChoiceSolution();
    this->showMultipleChoiceSolution();
}

void ExamAnswer::showSingleChoiceSolution()
{
    for(unsigned int tenTimes = 0 ; tenTimes < SINGLE_CHOICE_NUMBER / 10 ; tenTimes ++)
    {
        for(unsigned int index = 1; index <= 10 ; index ++)
        {
            int realNum = index + tenTimes*10;
            printf("[%02d] %-2s",realNum,getSingleAnswer(realNum).c_str());
        }
        printf("\n");
    }
}

void ExamAnswer::showMultipleChoiceSolution()
{
    for(unsigned int fiveTimes = 0 ; fiveTimes < MULTIPLE_CHOICE_NUMBER / 5 ; fiveTimes ++)
    {
        for(unsigned int index = 1; index <= 5 ; index ++)
        {
            int realNum = index + SINGLE_CHOICE_NUMBER + fiveTimes*5;
            printf("[%02d] %-7s",realNum,this->getMultipleAnswer(realNum).c_str());
        }
        printf("\n");
    }
}

bool ExamAnswer::setAnswer(unsigned int question_id,std::string answer)
{
    if(question_id <= 0 || question_id > SINGLE_CHOICE_NUMBER + MULTIPLE_CHOICE_NUMBER)
    {
        printf("id:%d not invalid\n",question_id);
        return false;
    }

    if( question_id <= SINGLE_CHOICE_NUMBER )
        return this->setSingleAnswer(question_id,answer);
    else
        return this->setMultipleAnswer(question_id,answer);
}

bool ExamAnswer::setSingleAnswer(unsigned int question_id,std::string answer)
{
    if(this->getSingleAnswer(question_id).compare(NO_ANSWER) != 0)
    {
        printf("id:%d is allread have answer\n",question_id);
        return false;
    }
    this->singleChoiceSolution[question_id] = answer;

    return true;
}

bool ExamAnswer::setMultipleAnswer(unsigned int question_id,std::string answer)
{
    std::vector<std::string>& result = this->multipleChoiceSolution[question_id];
    result.push_back(answer);
    return true;
}

const std::string ExamAnswer::getSingleAnswer(unsigned int question_id)
{
    if(question_id <= SINGLE_CHOICE_NUMBER)
    {
        return this->singleChoiceSolution[question_id];
    }

    return NO_ANSWER;
}

const std::string ExamAnswer::getMultipleAnswer(unsigned int question_id)
{
    std::string answer = NO_ANSWER;
    if(question_id > SINGLE_CHOICE_NUMBER  ||  question_id <= (SINGLE_CHOICE_NUMBER + MULTIPLE_CHOICE_NUMBER) )
    {
        std::vector<std::string>& result = this->multipleChoiceSolution[question_id];
        for(unsigned int i = 0 ; i < result.size() ; i ++)
        {
            answer+=result[i];
        }
    }
    return answer;
}

